home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtarray.cc < prev    next >
Encoding:
Text File  |  1994-09-06  |  7.9 KB  |  354 lines

  1. // CmTArray.cc
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Array template implementation.
  7. // -----------------------------------------------------------------
  8.  
  9.  
  10. // "CmTArray" is the default array constructor.
  11. //
  12. template <class T> CmTArray<T>::CmTArray(unsigned sz, unsigned dt)
  13. {
  14.   _delta   = dt;
  15.   _total   = 0;
  16.   _entries = (sz > 0) ? new T[sz] : NULL;
  17.   _size    = (_entries != NULL) ? sz : 0;
  18. }
  19.  
  20.  
  21. // "CmTArray" is the array copy constructor.
  22. //
  23. template <class T> CmTArray<T>::CmTArray(const CmTArray<T>& A)
  24.                               : CmTContainer<T>(A)
  25. {
  26.   _total   = 0;
  27.   _entries = NULL;
  28.   copy(A);
  29. }
  30.  
  31.  
  32. // "~CmTArray" is the array destructor.
  33. //
  34. template <class T> CmTArray<T>::~CmTArray()
  35. {
  36.   delete[] _entries;
  37. }
  38.  
  39.  
  40. // "=" assignment operator copies the specified array into this array.
  41. //
  42. template <class T> CmTArray<T>& CmTArray<T>::operator=(const CmTArray<T>& A)
  43. {
  44.   if (&A != this) copy(A);
  45.   return *this;
  46. }
  47.  
  48.  
  49. // "delta" sets a new delta value for automatic growing.
  50. //
  51. template <class T> void CmTArray<T>::delta(unsigned dt)
  52. {
  53.   _delta = dt;
  54. }
  55.  
  56.  
  57. // "delta" returns the current delta value.
  58. //
  59. template <class T> unsigned CmTArray<T>::delta() const
  60. {
  61.   return _delta;
  62. }
  63.  
  64.  
  65. // "total" returns the number of items in this array.
  66. //
  67. template <class T> int CmTArray<T>::total() const
  68. {
  69.   return _total;
  70. }
  71.  
  72.  
  73. // "at" returns the item at the specified index.
  74. //
  75. template <class T> const T& CmTArray<T>::at(int idx) const
  76. {
  77.   return _entries[idx];
  78. }
  79.  
  80.  
  81. // "[]" returns the item at the specified index.
  82. //
  83. template <class T> const T& CmTArray<T>::operator[](int idx) const
  84. {
  85.   return at(idx);
  86. }
  87.  
  88.  
  89. // "add" appends the specified item to this array.
  90. //
  91. template <class T> Bool CmTArray<T>::add(const T& rObj)
  92. {
  93.   if (_total >= _size)
  94.     if (_delta == 0 || !resize(_size+_delta))
  95.       return FALSE;
  96.   _entries[_total++] = rObj;
  97.   return TRUE;
  98. }
  99.  
  100.  
  101. // "insertAt" inserts the specified item at the specified index
  102. // shifting the other items to make room.
  103. //
  104. template <class T> Bool CmTArray<T>::insertAt(int idx, const T& rObj)
  105. {
  106.   if (idx < 0 || idx >= _total) return FALSE;
  107.   if (_total >= _size)
  108.     if (_delta == 0 || !resize(_size+_delta))
  109.       return FALSE;
  110.  
  111.   for (int ii = _total; ii > idx; ii--)
  112.     _entries[ii] = _entries[ii-1];
  113.   _entries[idx] = rObj;
  114.   _total++;
  115.   return TRUE;
  116. }
  117.  
  118.  
  119. // "replaceAt" replaces the item at the specified index with the
  120. // specified item.
  121. //
  122. template <class T> Bool CmTArray<T>::replaceAt(int idx, const T& rObj)
  123. {
  124.   if (idx < 0 || idx >= _total) return FALSE;
  125.   _entries[idx] = rObj;
  126.   return TRUE;
  127. }
  128.  
  129.  
  130. // "remove" removes the first occurrence of an item equal to the specified
  131. // item in the array.
  132. //
  133. template <class T> Bool CmTArray<T>::remove(const T& rObj)
  134. {
  135.   if (_total == 0) return FALSE;
  136.   int  ii    = 0;
  137.   Bool found = FALSE;
  138.   while (ii < _total && !found)
  139.     if (_entries[ii++] == rObj) found = TRUE;
  140.   return (found) ? removeAt(ii-1) : FALSE;
  141. }
  142.  
  143.  
  144. // "removeAt" removes the item at the specified index.
  145. //
  146. template <class T> Bool CmTArray<T>::removeAt(int idx)
  147. {
  148.   if (idx < 0 || idx >= _total) return FALSE;
  149.  
  150.   for (int ii = idx; ii < _total-1; ii++)
  151.     _entries[ii] = _entries[ii+1];
  152.   _total--;
  153.   return TRUE;
  154. }
  155.  
  156.  
  157. // "index" returns the index of the first occurrence of an item equal
  158. // to the specified item.
  159. //
  160. template <class T> int CmTArray<T>::index(const T& rObj) const
  161. {
  162.   int  ii  = 0;
  163.   int  idx = -1;
  164.   while (ii < _total && idx == -1)
  165.     if (_entries[ii++] == rObj) idx = ii-1;
  166.   return idx;
  167. }
  168.  
  169.  
  170. // "lookup" returns the first occurrence of the item equal to the
  171. // specified item in the array.
  172. //
  173. template <class T> const T& CmTArray<T>::lookup(const T& rObj) const
  174. {
  175.   int idx = index(rObj);
  176.   return (idx > -1) ? _entries[idx] : _error;
  177. }
  178.  
  179.  
  180. // "contains" checks the array for an item equal to the input.
  181. //
  182. template <class T> Bool CmTArray<T>::contains(const T& rObj) const
  183. {
  184.   return (index(rObj) > -1) ? TRUE : FALSE;
  185. }
  186.  
  187.  
  188. // "occurrences" counts the number of occurrences of items equal to the
  189. // specified item.
  190. //
  191. template <class T> unsigned CmTArray<T>::occurrences(const T& rObj) const
  192. {
  193.   int      ii  = 0;
  194.   unsigned num = 0;
  195.   while (ii < _total)
  196.     if (_entries[ii++] == rObj) num++;
  197.   return num;
  198. }
  199.  
  200.  
  201. // "removeAll" sets the number of items in this array to zero.
  202. //
  203. template <class T> void CmTArray<T>::removeAll()
  204. {
  205.   _total = 0;
  206. }
  207.  
  208.  
  209. // "resize" resizes the storage allocated for this array.
  210. //
  211. template <class T> Bool CmTArray<T>::resize(unsigned newSize)
  212. {
  213.   if (newSize == 0)
  214.   {
  215.     delete[] _entries;
  216.     _entries = NULL;
  217.     _size    = 0;
  218.     _total   = 0;
  219.     return TRUE;
  220.   }
  221.  
  222.   T *newEntries = new T[newSize];
  223.   if (!newEntries) return FALSE;
  224.  
  225.   if (newSize < _total) _total = newSize;
  226.  
  227.   for (int ii = 0; ii < _total; ii++)
  228.     newEntries[ii] = _entries[ii];
  229.  
  230.   delete[] _entries;
  231.   _entries = newEntries;
  232.   _size    = newSize;
  233.   return TRUE;
  234. }
  235.  
  236.  
  237. // "isEmpty" returns whether or not this array has any items in it.
  238. //
  239. template <class T> Bool CmTArray<T>::isEmpty() const
  240. {
  241.   return (_total == 0);
  242. }
  243.  
  244.  
  245. // "quickSort" uses the standard library to perform a quick sort
  246. // on this array.
  247. //
  248. template <class T> void CmTArray<T>::quickSort()
  249. {
  250.   qsort(_entries, _total, sizeof(T), &CmTArray<T>::cmpObjs);
  251. }
  252.  
  253.  
  254. // "newIterator" creates and returns a new array iterator.
  255. //
  256. template <class T> CmTIterator<T>* CmTArray<T>::newIterator() const
  257. {
  258.   return new CmTArrayIterator<T>(*this);
  259. }
  260.  
  261.  
  262. // "copy" copies the items from the specified array into this array.
  263. //
  264. template <class T> void CmTArray<T>::copy(const CmTArray<T>& A)
  265. {
  266.   _size  = A._size;
  267.   _total = A._total;
  268.   _delta = A._delta;
  269.  
  270.   delete[] _entries;
  271.   _entries = (_size > 0) ? new T[_size] : NULL;
  272.   if (!_entries)
  273.   {
  274.     _total = 0;
  275.     _size  = 0;
  276.   }
  277.   else
  278.   {
  279.     for (int ii = 0; ii < _total; ii++)
  280.       _entries[ii] = A._entries[ii];
  281.   }
  282. }
  283.  
  284.  
  285. // "cmpObjs" is called by the standard library qsort function for
  286. // sorting the array into ascending order.
  287. //
  288. template <class T> int CmTArray<T>::cmpObjs(const void* obj1, const void* obj2)
  289. {
  290.   T* t1 = (T*) obj1;
  291.   T* t2 = (T*) obj2;
  292.   return ((*t1 == *t2) ? 0 : ((*t1 > *t2) ? 1 : -1));
  293. }
  294.  
  295.  
  296. // "CmTArrayIterator" is the iterator constructor.
  297. //
  298. template <class T> CmTArrayIterator<T>::CmTArrayIterator(const CmTArray<T>& A)
  299.                                        : _array(A), _index(0)
  300. { }
  301.  
  302.  
  303. // "done" returns whether or not this iterator can advance any further.
  304. //
  305. template <class T> Bool CmTArrayIterator<T>::done() const
  306. {
  307.   return (_index >= _array._total || _index < 0);
  308. }
  309.  
  310.  
  311. // "next" returns the current item in the array and advances the
  312. // iterator to the next item.
  313. //
  314. template <class T> const T& CmTArrayIterator<T>::next()
  315. {
  316.   if (_index < _array._total) return _array._entries[_index++];
  317.   else                        return _array._error;
  318. }
  319.  
  320.  
  321. // "previous" returns the current item in the array and decrements the
  322. // iterator to the previous item.
  323. //
  324. template <class T> const T& CmTArrayIterator<T>::previous()
  325. {
  326.   if (_index >= 0) return _array._entries[_index--];
  327.   else             return _array._error;
  328. }
  329.  
  330.  
  331. // "current" returns the current array item.
  332. //
  333. template <class T> const T& CmTArrayIterator<T>::current() const
  334. {
  335.   if (_index < _array._total) return _array._entries[_index];
  336.   else                        return _array._error;
  337. }
  338.  
  339.  
  340. // "first" moves the iterator to the first item.
  341. //
  342. template <class T> void CmTArrayIterator<T>::first()
  343. {
  344.   _index = 0;
  345. }
  346.  
  347.  
  348. // "last" moves the iterator to the last item.
  349. //
  350. template <class T> void CmTArrayIterator<T>::last()
  351. {
  352.   _index = _array._total - 1;
  353. }
  354.